package com.mesd.killer.ui.renderer;

import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URI;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableCellEditor;

import com.mesd.killer.DetailGame;
import com.mesd.killer.model.GameResult;
import com.mesd.killer.table.FTable;
import com.mesd.killer.ui.cellEditor.ButtonEditor;
import com.mesd.killer.ui.cellEditor.CheckBoxEditor;
import com.mesd.killer.ui.cellEditor.ITableConstants;
import com.mesd.killer.ui.cellEditor.LabelEditor;
import com.mesd.killer.ui.tools.MyJLabel;

public class GridBagTableCellRenderer extends DefaultTableCellRenderer
{

	@Override
	public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
		boolean hasFocus, int row, int column)
	{
		try
		{
			TableCellEditor cellEditor = table.getCellEditor(
				row, column);
			Component editorComp = cellEditor.getTableCellEditorComponent(
				table, value, isSelected, row, column);
			
			if (cellEditor instanceof CheckBoxEditor)
			{
				value = value == null ? new Boolean(false): new Boolean(true);
				JCheckBox checkBox = (JCheckBox) editorComp;
				checkBox.setSelected(((Boolean) value).booleanValue());
				return editorComp;
			}
			else if (cellEditor instanceof LabelEditor)
			{
				MyJLabel label = (MyJLabel) editorComp;
				label.setText(ITableConstants.FENXI_OUYA);
				label.setUrl(new URI("http://www.baidu.com"));
				return label;
			}
			else if (cellEditor instanceof ButtonEditor)
			{
				JButton btn = (JButton) editorComp;
				btn.setText(ITableConstants.FENXI_KILLER);
				final JTable tbJTable = table;
				final int tbRow = row;
				btn.addActionListener(new ActionListener()
				{
					@Override
					public void actionPerformed(ActionEvent e)
					{
						if(tbJTable instanceof FTable)
						{
							FTable fTable = (FTable)tbJTable;
							int selectedRow = fTable.getSelectedRow();
							if(selectedRow == tbRow)
							{
								Object object = fTable.getSelectedObject();
								if(object instanceof GameResult)
								{
									new DetailGame((GameResult)object);
								}
							}
						}
					}
				});

				return btn;
			}
		}
		catch (Exception ee)
		{
		}

		return super.getTableCellRendererComponent(
			table, value, isSelected, hasFocus, row, column);

	}
}
